<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getCompanyDepartments, postDepartments, putCompanyDepartments } from '@/api/departments'
import { getUserSimple } from '@/api/employees'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validtateCode = (rule, value, callback) => {
      //  部门编码不能相同
      let codeList = null
      // 如果是编辑
      if (this.isEdit) {
        codeList = this.originList.filter(item => item.id !== this.id).map(({ code }) => code)
      } else {
        codeList = this.originList.map(({ code }) => code)
      }
      codeList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    const validtateName = (rule, value, callback) => {
      let nameList = null
      // 如果是编辑
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.id).pid
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id).map(({ name }) => name)
      } else {
        nameList = this.originList.filter(item => item.pid === this.id).map(({ name }) => name)
      }
      nameList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { validator: validtateName, trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { validator: validtateCode, trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: ['blur', 'change'] },
          { min: 2, max: 200, message: '请输入 2 ~ 200 位字符', trigger: ['blur', 'change'] }
        ]
      },
      list: []
    }
  },
  computed: {
  },
  created() {
    this.loadUserSimple()
    this.loadDepartmentDetail()
  },
  methods: {
    // 获取员工的 ID 和 姓名
    async loadUserSimple() {
      try {
        const res = await getUserSimple()
        // console.log(res.data)
        this.list = res.data
      } catch (e) {
        console.log(e)
      }
    },
    // 提交表单
    async hSubmit() {
      let res = null
      try {
        // 添加
        if (!this.isEdit) {
          this.form.pid = this.id
          res = await postDepartments(this.form)
          // console.log(res)
        } else {
          // 修改
          console.log(this.form)
          res = await putCompanyDepartments(this.form)
          // console.log(res)
        }
        this.$message.success(res.message)
        this.$emit('success')
        // this.form = res.data
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hCancel() {
      this.$emit('hCancels')
    },
    // 获取数据渲染在 dialog 上
    async loadDepartmentDetail() {
      if (!this.isEdit) return
      const res = await getCompanyDepartments(this.id)
      // console.log(res)
      this.form = res.data
    }

  }
}
</script>

<style scoped>
</style>
